package pro.team.utils;

import java.io.Serializable;

/**
 * 统一返回前端数据
 * @author liuxiaowei
 * @date 2020/11/5 16:57
 */
public class R implements Serializable {
	private static final long serialVersionUID = 1L;

	private int code;

	private String msg;

	private Object data;

	public R() {
		this.code = 200;
		this.msg = "success";
	}

	public R(int code,String msg) {
		this.code = code;
		this.msg = msg;
	}

	public R(int code,String msg,Object data) {
		this.code = code;
		this.msg = msg;
		this.data = data;
	}

	public static R error() {
		return error(500, "未知异常");
	}

	public static R error(String msg) {
		return error(500, msg);
	}

	public static R error(int code, String msg) {
		return new R(code,msg);
	}

	public static R ok(String msg) {
		R r = new R();
		r.setMsg(msg);
		return r;
	}

	public static R ok(Object data) {
		R r = new R();
		r.setData(data);
		return r;
	}

	public static R ok(String msg,Object data) {
		R r = new R();
		r.setMsg(msg);
		r.setData(data);
		return r;
	}

	public static R ok() {
		return new R();
	}

	public boolean success() {
		return this.code == 200;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}
}
